import Flutter
import UIKit
import SystemConfiguration.CaptiveNetwork


public class SwiftTearchPlugin: NSObject, FlutterPlugin,UIDocumentInteractionControllerDelegate {
    
    let controllers = UIApplication.shared.delegate?.window??.rootViewController!;
    
    var resultData: FlutterResult? = nil;
    
    public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        return controllers!;
    }
    
    
    public static func register(with registrar: FlutterPluginRegistrar) {
        let channel = FlutterMethodChannel(name: "tearch_plugin", binaryMessenger: registrar.messenger())
        let instance = SwiftTearchPlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
     }
    

    
    public func detachFromEngine(for registrar: FlutterPluginRegistrar) {

    }
    
    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        
        self.resultData = result;
    
        let method = call.method;
        
        ///预览文件
        if(method=="previewFile"){
            
            var filePath:String?
            
            if let _arguments = call.arguments as? [String : Any] {
                filePath = _arguments["file_path"] as? String;
            }
            
            previewFile(filePath: filePath)
            
        }
        
        ///打开应用市场
        if(method=="openMarket"){
            var urlString:String?
            if let _arguments = call.arguments as? [String : Any] {
                urlString = _arguments["packageName"] as? String;
            }
            
            gotoAppStore(urlStr: urlString)
        }
        
        ///获取mac地址
        if(method=="getBssid"){
            result(getWiFiBSsid())
        }
        
    }
    
    /**
     预览文件
     */
    func previewFile(filePath:String?){
        if(filePath==nil){
            return
        }
        let url = URL(fileURLWithPath: filePath!)
        //页面加载出错了，我们启用第三方打开
        let docController=UIDocumentInteractionController(url:url)
        docController.name = url.lastPathComponent
        docController.delegate = self;
        docController.presentPreview(animated: true)
    }
    
    /**
     跳转到应用AppStore页面
     */
    func gotoAppStore(urlStr:String?) {
        if(urlStr==nil){
            return
        }
        
        
        let url = NSURL(string: "itms-apps://itunes.apple.com/app/id1406237249?action=write-review")
        UIApplication.shared.openURL(url! as URL)
    }
    
      ///获取WiFimac地址
    func getWiFiBSsid() -> String? {
        var ssid: String?
        if let interfaces = CNCopySupportedInterfaces() as NSArray? {
            for interface in interfaces {
                if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
                    ssid = interfaceInfo[kCNNetworkInfoKeyBSSID as String] as? String
                    break
                }
            }
        }
        return ssid
    }
}
